﻿using System;
using System.Data;
using System.Data.Sql;
using System.Windows.Forms;

namespace CProd
{
    public partial class frmSql : Form
    {
        public frmSql()
        {
            InitializeComponent();
            this.cbDB.GotFocus += new EventHandler(this.cbDB_GotFocus);
        }

        private void frmSql_Load(object sender, EventArgs e)
        {
            if (!Properties.Settings.Default.LocalChecked)
                this.chkLocal.Checked = false;
            else
                this.chkLocal.Checked = true;
            Application.DoEvents();
            GetSqlServers();
        }

        #region "Get Sql Servers"

        private void GetSqlServers()
        {
            DataTable sqlServersTable = SqlDataSourceEnumerator.Instance.GetDataSources();

            foreach (DataRow rowOfData in sqlServersTable.Rows)
            {
                string serverName = rowOfData["ServerName"].ToString();
                string instanceName = rowOfData["InstanceName"].ToString();
                if (!instanceName.Equals(string.Empty))
                {
                    serverName = serverName + "\\" + instanceName;
                }
                this.cbServers.Items.Add(serverName);
            }
        }

        #endregion

        private void btConnect_Click(object sender, EventArgs e)
        {
            if (this.chkLocal.Checked)
            {
                Properties.Settings.Default.conStr = "data source=" + this.cbServers.SelectedItem.ToString() + "; Initial Catalog="
                    + this.cbDB.SelectedItem.ToString() + "; integrated security=sspi; connection timeout=7";
            }
            else if ((!string.IsNullOrEmpty(this.txtUser.Text)) && (!string.IsNullOrEmpty(this.txtPass.Text)))
            {
                Properties.Settings.Default.conStr = "data source=" + this.cbServers.SelectedItem.ToString() + "; Initial Catalog="
                    + this.cbDB.SelectedItem.ToString() + "; user id=" + this.txtUser.Text + "; password=" + this.txtPass.Text + ";connection timeout=7";
            }
            Properties.Settings.Default.Save();
            frmEntrada.Conn.ConnectionString = Properties.Settings.Default.conStr;
            this.Close();
        }

        protected void cbDB_GotFocus(object sender, EventArgs e)
        {
            if (this.chkLocal.Checked)
                Properties.Settings.Default.conStr = "Data Source=" + this.cbServers.SelectedItem.ToString() +
                    ";Integrated Security=SSPI; Connection Timeout=7";
            else if ((!string.IsNullOrEmpty(this.txtUser.Text)) && (!string.IsNullOrEmpty(this.txtPass.Text)))
                Properties.Settings.Default.conStr = "Data Source=" + this.cbServers.SelectedItem.ToString() +
                    "; User ID=" + this.txtUser.Text + "; Password=" + this.txtPass.Text + ";Connection Timeout=7";
            frmEntrada.Conn.ConnectionString = Properties.Settings.Default.conStr;
            GetDatabases();
        }

        protected void GetDatabases()
        {
            try
            {
                frmEntrada.Conn.Open();
                DataTable tblDatabases = frmEntrada.Conn.GetSchema("Databases");
                foreach (DataRow row in tblDatabases.Rows)
                {
                    this.cbDB.Items.Add(row["database_name"].ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            frmEntrada.Conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Form frm in Application.OpenForms)
                frm.Close();
            Application.Exit();
        }

    }
}
